package Servlet;

import Bean.Aircraft;
import Service.AircraftService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-14
 * Time: 17:52
 */
@WebServlet("/AddAircraftServlet")
public class AddAircraftServlet extends HttpServlet {
    private AircraftService aircraftService = new AircraftService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf8");
        String aircraftId = req.getParameter("aircraftId");
        String model = req.getParameter("model");
        String currentStatus = req.getParameter("currentStatus");
        int maxSeats = Integer.parseInt(req.getParameter("maxSeats"));
        Aircraft aircraft = new Aircraft();
        aircraft.setAircraftId(aircraftId);
        aircraft.setModel(model);
        aircraft.setCurrentStatus(Aircraft.AircraftStatus.valueOf(currentStatus));
        aircraft.setMaxSeats(maxSeats);
        boolean ok  = aircraftService.insert(aircraft);
        if(!ok) {
            req.setAttribute("status", "error");
            req.setAttribute("message", "录入失败,检查是有重复编号");
            req.setAttribute("redirectUrl", "aircraft_insert.jsp");
            req.setAttribute("waitSeconds", 2);
            req.getRequestDispatcher("skip.jsp").forward(req,resp);
        }
        req.setAttribute("status", "success");
        req.setAttribute("message", "录入成功!!");
        req.setAttribute("redirectUrl", "aircraft_insert.jsp");
        req.setAttribute("waitSeconds", 2);
        req.getRequestDispatcher("skip.jsp").forward(req,resp);
    }
}
